dofile("Map.lua")
dofile("Char.lua")
dofile("Monster.lua")

Game = {
	map,
	units = {},
	nunits = 0,
	monsters = {},
	nmonsters = 0,		
	characters = {},
	ncharacters = 0,
	randomGenerator
}

function Game:new(o)
	o = o or {}
	setmetatable(o, self)
    self.__index = self
    
    self.map = Map:new()
    
    return o	
end

function Game:init()
	self.randomGenerator = math.randomseed(os.date("%S"))
	self.map:loadImages()
	self.map:loadMapFile("mapa.txt")
	--inserindo inimigos
	dofile("scripts/garimpeiro.lua")	
end

function comp(a, b)
	if a:get_y() > b:get_y() then
		return true
	end
	return false
end

function Game:render()
	table.sort(self.units, comp)
	self.map:render(ImageCanvas)
	local i = 1
	while(i <= self.nunits) do
		self.units[i]:render(ImageCanvas)
		i = i + 1
	end
end

function Game:addMonster(monster)
	self.nmonsters = self.nmonsters + 1
	self.nunits = self.nunits + 1
	monster:set_id(self.nmonsters)
	self.monsters[self.nmonsters] = monster
	self.units[self.nunits] = monster

end

function Game:removeMonster(monster)
	local i = monster:get_id()
	while(i < self.nmonsters) do
		self.monsters[i] = self.monsters[i + 1]
		self.monsters[i]:set_id(i)
		i = i + 1
	end
	-- remove o ultimo
	self.monsters[self.nmonsters] = nil
	self.nmonsters = self.nmonsters - 1
	--self:removeUnit(monster)	
end

function Game:addCharacter(char)
	self.ncharacters = self.ncharacters + 1
	self.nunits = self.nunits + 1
	char:set_id(self.ncharacters)
	self.characters[self.ncharacters] = char
	self.units[self.nunits] = char
end

function Game:removeCharacter(char)
	local i = char:get_id()
	while(i < self.ncharacters) do
		self.characters[i] = self.characters[i + 1]
		self.characters[i]:set_id(i)
		i = i + 1
	end
	-- remove o ultimo
	self.characters[self.ncharacters] = nil
	self.ncharacters = self.ncharacters - 1
	--self:removeUnit(char)	
end

function Game:removeUnit(unit)
	local i = unit:get_id()
	while(i < self.nunits) do
		self.units[i] = self.units[i + 1]
		self.units[i]:set_id(i)
		i = i + 1
	end
	-- remove o ultimo
	self.units[self.nunits] = nil
	self.nunits = self.nunits - 1	
end

function Game:get_randomGenerator()
	return self.randomGenerator
end

function Game:dice_roll(n)
	return math.random(n)
end

function Game:get_map()
	return self.map
end

function Game:update()
	-- ordena personagens e monstros.
	
	local i = 1
	while(i <= self.nmonsters) do
		self.monsters[i]:move()
		self.monsters[i]:attack()
		if (self.monsters[i]:get_dead_body_time() <= 0) then
			self:removeMonster(self.monsters[i])
		end
		i = i + 1
	end
	i = 1
	while(i <= self.ncharacters) do
		self.characters[i]:move()
		self.characters[i]:attack()
		if (self.characters[i]:get_dead_body_time() <= 0) then
			self:removeCharacter(self.characters[i])
		end
		i = i + 1
	end
	
	--Rotina de teste de combate
	if (self.monsters[1]) then
		self.monsters[1]:set_target(self.characters[1])
	end
	--self.characters[1]:attack(self.monsters[1])  
end

function Game:isSomeoneAt(x, y)
	local i = 1
	y = cd.UpdateYAxis(y)
	while (i <= self.nunits) do
		if (self.units[i]:get_x()-10 < x) and ( x <= self.units[i]:get_x()+10) and
		   (self.units[i]:get_y()-10 < y) and ( y <= self.units[i]:get_y()+10) then
			return self.units[i]
		else
			self.units[i]:unmark()
		end
		i = i + 1
	end
	return nil 
end